//
//  DrawingBoardView.m
//  Photoshop
//
//  Created by 鑫龙 on 16/4/13.
//  Copyright © 2016年 鑫龙. All rights reserved.
//

#import "DrawingBoardView.h"

@implementation DrawingBoardView

- (NSMutableArray *)array {
    if (!_array) {
        _array = [NSMutableArray new];
    }
    return _array;
}


//嗨音
//自定义View划线
- (void)drawRect:(CGRect)rect {
    
    //Mac的frame和ios的frame不同，做一下垂直映射
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(ctx, 0, rect.size.height);
    CGContextScaleCTM(ctx, 1, -1);
    if (self.array.count < 1) {
        return;
    }
    //实例化路径对象
    UIBezierPath * path = [[UIBezierPath alloc] init];
    
    //取出第一个点
    CGPoint point = CGPointFromString(self.array.firstObject);
    //设置路径对象上的点
    [path moveToPoint:point];
    
    //添加线条到路径
    for (int i = 1; i < self.array.count; i++) {
        NSString * pointString = self.array[i];
        CGPoint point = CGPointFromString(pointString);
        
        //给路径添加线条
        [path addLineToPoint:point];
    }
    
    //将线条画出来
    NSLog(@"画出了%ld条线条",self.array.count - 1);
    [path stroke];
}
- (void)addPoint:(NSString *)pointString{
    [self.array addObject:pointString];
    
    //没添加一个点，重绘view
    //每对一个view调用setNeedsDisplay方法，view会重新调用drawRect方法重绘界面
    [self setNeedsDisplay];
    
}

@end
